package com.manage.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.manage.platform.context.UserContext;
import com.manage.platform.dto.AchieveApprovalDto;
import com.manage.platform.dto.AchieveDto;
import com.manage.platform.dto.FirstPageAchieveDto;
import com.manage.platform.dto.query.AchievePageQuery;
import com.manage.platform.entity.AchieveEntity;
import com.manage.platform.mapper.AchieveMapper;
import com.manage.platform.utils.RPage;
import com.manage.platform.vo.AchieveDetailsVo;
import com.manage.platform.vo.AchievePartVo;
import com.manage.platform.vo.AchieveVo;
import com.manage.platform.vo.UserVo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * YWD
 * 2025/9/29
 */
@AllArgsConstructor
@Service
public class AchieveServiceImpl extends ServiceImpl<AchieveMapper, AchieveEntity> {

    private final AchieveIndicatorSystemServiceImpl achieveIndicatorSystemServiceImpl;
    private final AchieveTaskServiceImpl achieveTaskServiceImpl;
    private final AchieveOrganizeServiceImpl achieveOrganizeServiceImpl;


    public RPage<List<AchieveVo>> getAchievePage(AchievePageQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        List<AchieveVo> list = this.baseMapper.getAchievePage(query);
        return RPage.ok(list, query.getPage(), query.getLimit(), page.getTotal());
    }

    public AchieveDetailsVo getAchieveDetails(Long id) {
        return this.baseMapper.getAchieveDetails(id);
    }

    public Boolean saveAchieve(AchieveDto dto) {
        UserVo userVo = UserContext.getUser();
        dto.setUserId(userVo.getId());
        return save(BeanUtil.copyProperties(dto, AchieveEntity.class));
    }

    public Boolean updateAchieve(AchieveDto dto) {
        return updateById(BeanUtil.copyProperties(dto, AchieveEntity.class));
    }

    public Boolean deleteAchieve(Long id) {
        return removeById(id);
    }

    public Boolean deleteBatchAchieve(List<Long> ids) {
        return removeByIds(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean approvalAchieve(AchieveApprovalDto dto) {
        UserVo userVo = UserContext.getUser();
        dto.setApprovalId(userVo.getId());
        dto.setApprovalDate(LocalDateTime.now().toString());
        AchieveEntity entity = this.getById(dto.getId());
        BeanUtil.copyProperties(dto, entity);
        //关联数据
        achieveIndicatorSystemServiceImpl.removeByAchieveId(dto.getId());
        if (CollectionUtil.isNotEmpty(dto.getIndicatorSystemList())) {
            achieveIndicatorSystemServiceImpl.saveList(dto.getId(), dto.getIndicatorSystemList());
        }
        achieveTaskServiceImpl.removeByAchieveId(dto.getId());
        if (CollectionUtil.isNotEmpty(dto.getTaskList())) {
            achieveTaskServiceImpl.saveList(dto.getId(), dto.getTaskList());
        }
        achieveOrganizeServiceImpl.removeByAchieveId(dto.getId());
        if (CollectionUtil.isNotEmpty(dto.getOrganizeList())) {
            achieveOrganizeServiceImpl.saveList(dto.getId(), dto.getOrganizeList());
        }
        return updateById(entity);
    }


    public List<AchievePartVo> getAchieveHistoryList(FirstPageAchieveDto dto) {
        //根据输入动态展示
        List<AchievePartVo> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(dto.getIndicatorSystemList())) {
            AchievePartVo partVo = this.baseMapper.getAchieveHistoryListOne(dto.getIndicatorSystemList());
            if (ObjectUtil.isNotEmpty(partVo)) {
                list.add(partVo);
            }
        }
        if (CollectionUtil.isNotEmpty(dto.getTaskList())) {
            AchievePartVo partVo = this.baseMapper.getAchieveHistoryListTwo(dto.getTaskList());
            if (ObjectUtil.isNotEmpty(partVo)) {
                list.add(partVo);
            }
        }
        if (CollectionUtil.isNotEmpty(dto.getOrganizeList())) {
            AchievePartVo partVo = this.baseMapper.getAchieveHistoryListThree(dto.getOrganizeList());
            if (ObjectUtil.isNotEmpty(partVo)) {
                list.add(partVo);
            }
        }
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        return list;
    }


}
